package cn.edu.csust.coursecard.service.impl;

import cn.edu.csust.coursecard.dao.entity.Advice;
import cn.edu.csust.coursecard.common.CodeEnum;
import cn.edu.csust.coursecard.common.ReturnData;
import cn.edu.csust.coursecard.dao.AdviceMapper;
import cn.edu.csust.coursecard.exception.BaseException;
import cn.edu.csust.coursecard.service.AdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author zsw
 * @date 2019/12/18 16:18
 */
@Service("adviceService")
public class AdviceServiceImpl implements AdviceService {

	@Autowired
	AdviceMapper adviceMapper;

	@Override
	public ReturnData addAdvice(HttpServletRequest request, Advice advice) {
		Integer userId = Integer.parseInt(request.getAttribute("userId").toString());
		advice.setUserId(userId);
		advice.setCreateTime(new Date());
		advice.setUpdateTime(new Date());
		advice.setIsValid((byte) 1);
		adviceMapper.insertSelective(advice);
		return ReturnData.success();
	}

	@Override
	public ReturnData getAdvice(HttpServletRequest request, Integer id) {
		return ReturnData.success(adviceMapper.selectByPrimaryKey(id));
	}

	@Override
	public ReturnData getAllAdvice(HttpServletRequest request) {
		return ReturnData.success(adviceMapper.selectAll());
	}

	@Override
	public ReturnData deleteAdvice(HttpServletRequest request, Integer id) {
		int row = adviceMapper.deleteByPrimaryKey(id);
		if(row != 1){
			throw new BaseException(CodeEnum.REQUEST_FAILED.getCode(), "反馈删除失败！");
		}
		return ReturnData.success();
	}

	@Override
	public ReturnData deleteAllAdvice(HttpServletRequest request) {
		adviceMapper.deleteAll();
		return ReturnData.success();
	}
}
